/*
+--------------------------------------------------------------------------
|   Mblog [#RELEASE_VERSION#]
|   ========================================
|   Copyright (c) 2014, 2015 mtons. All Rights Reserved
|   http://www.mtons.com
|
+---------------------------------------------------------------------------
*/
package com.hefei.blog.web.controller.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hefei.blog.base.data.Data;
import com.hefei.blog.core.biz.PostBiz;
import com.hefei.blog.core.data.Post;
import com.hefei.blog.core.data.Tag;
import com.hefei.blog.core.data.User;
import com.hefei.blog.core.persist.service.TagService;
import com.hefei.blog.core.persist.service.UserService;
import com.hefei.blog.web.auth.service.IAuthService;
import com.hefei.blog.web.controller.BaseController;
import com.hefei.common.returncode.ReturnCode;
import com.hefei.frontend.framework.http.request.CommonParameterThreadLocal;

/**
 * 侧边栏数据加载
 * 
 * @author langhsu
 *
 */
@Controller
@RequestMapping("/api")
public class SidebarController extends BaseController {
	@Autowired
	private PostBiz postBiz;
	@Autowired
	private TagService tagService;

	@Autowired
	private UserService userService;
	
	@Autowired
	private IAuthService authService;
	
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public @ResponseBody Data login(String username, String password, ModelMap model
			,HttpServletRequest request, HttpServletResponse response) {
		Data data = Data.failure("操作失败");

		if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
			return data;
		}
		String returnCode = authService.login(username, password, request, response);
		
		if (!ReturnCode.RETURN_CODE_SUCCESS.equals(returnCode)) {
			data.setMessage("用户名或密码错误");
			return data;
		}
//		data = Data.success("登录成功", getSubject(request));
		data = Data.success("登录成功", CommonParameterThreadLocal.getUserCookie());
		return data;
	}

	@RequestMapping("/latests.json")
	public @ResponseBody List<Post> latests(HttpServletRequest request) {
		
		List<Post> rets = postBiz.findRecents(6, CommonParameterThreadLocal.getUserId());
		return rets;
	}
	
	@RequestMapping("/hots.json")
	public @ResponseBody List<Post> hots(HttpServletRequest request) {
		Object obj = postBiz.getPost(1L);
		List<Post> rets = postBiz.findHots(6, CommonParameterThreadLocal.getUserId());
		return rets;
	}
	
	@RequestMapping("/hot_tags.json")
	public @ResponseBody List<Tag> hotTags() {
		List<Tag> rets = tagService.topTags(12, false);
		return rets;
	}
	
	/**
	 * 我的粉丝
	 * @param pn
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/Userfans.json")
	public @ResponseBody List<User>  fans(Integer pn) {
		
		List<User> rets = userService.getHotUserByfans(12);
         return rets;
	}
}
